<?php

use think\facade\Db;

//获取栏目
function getCates($pid = 0){
    $lists = Db::name('articles_cate')->where('pid',$pid)->order('id asc')->select()->toArray();
    return $lists;
}
//判断是否有子栏目
function hasChildCate($pid){
    $row = Db::name('articles_cate')->where('pid',$pid)->limit(1)->find();
    if(empty($row)){
        return false;
    }
    return true;
}
//获取多级子栏目ID
function getChildCateIds($pid,$ids=''){
    $rows = Db::name('articles_cate')->field('id')->where('pid',$pid)->select()->toArray();
    foreach($rows as $v){
        $ids .= $v['id'].',';
        if(hasChildCate($v['id'])){
            return getChildCateIds($v['id'],$ids);
        }
    }
    return $ids;
}
//获取栏目途径
function getCatePath($cate,$cates = []){
    $cates[] = $cate;
    if($cate['pid'] == 0){
        return array_reverse($cates);
    }else{
        $cate = Db::name('articles_cate')->where('id',$cate['pid'])->find();
        return getCatePath($cate,$cates);
    }
}
//获取地区
function getAreas($pid = 0){
    $lists = Db::name('area')->where([
        ['pid','=',$pid],
        ['pinyin','<>','']
    ])->order('id asc')->select()->toArray();
    return $lists;
}
//随机获取地区
function getAreasByRand($limit = 10, $level = 2){
    $lists = Db::name('area')->where([
        ['level','=',$level],
        ['pinyin','<>','']
    ])->orderRaw('rand()')->limit($limit)->select()->toArray();
    return $lists;
}
//获取热门地区
function getAreasByHot($limit = 10, $skip = 0, $level = 2){
    $lists = Db::name('area')->where([
        ['is_hot','=',1],
        ['level','=',$level],
        ['pinyin','<>','']
    ])->order('sort','asc')->limit($skip, $limit)->select()->toArray();
    return $lists;
}
//获取城市 根据首字母
function getCitysByFirst($first){
    $lists = Db::name('area')->where([
        ['level','=',2],
        ['first','=',$first],
        ['pinyin','<>','']
    ])->select()->toArray();
    return $lists;
}
//获取城市
function getCitysByChar($limit,$len=2){
    $lists = Db::name('area')->where('level',2)->where('char_length(shortname) = '.$len)->limit($limit)->select()->toArray();
    return $lists;
}
//获取地区拼音
function getAreaPy($id){
    $row = Db::name('area')->field('pinyin')->find($id);
    return $row['pinyin'];
}
function getModelTable($id){
    $row = Db::name('articles_models')->field('tablename')->find($id);
    return $row['tablename'];
}
//获取内容列表
function getArclist($param){
    if(empty($param['table'])) return;
    if(empty($param['where'])) $param['where'] = '';
    if(empty($param['order'])) $param['order'] = 'id desc';
    if(empty($param['skip'])) $param['skip'] = 0;
    if(empty($param['limit'])) $param['limit'] = 10;
    $lists = Db::name($param['table'])->where($param['where'])->orderRaw($param['order'])->limit($param['skip'],$param['limit'])->select()->toArray();
    foreach($lists as $k => $v){
        $lists[$k]['url'] = makeArcUrl($v['id'],$v['cate_id'],$v['areaid1'],$v['areaid2']);
        if(empty($v['litpic'])){
            $lists[$k]['litpic'] = '/default.jpg';
        }
    }
    return $lists;
}
//生成URL
function makeArcUrl($id,$cate_id,$areaid1,$areaid2){
    $url = '';
    if(empty($id) || empty($cate_id) || empty($areaid1) || empty($areaid2)){
        return $url;
    }
    $cate = Db::name('articles_cate')->field('dirname')->find($cate_id);
    $area1 = Db::name('area')->field('pinyin')->find($areaid1);
    $area2 = Db::name('area')->field('pinyin')->find($areaid2);
    $url = '/'.$area1['pinyin'].'_'.$area2['pinyin'].'/'.$cate['dirname'].'/'.$id.'.html';
    return $url;
}
//生成分页
function makePages($url,$page,$total,$count){
    if($count == 0){
        return;
    }
    $ext = 'p';
    if($page == 1){
        $homePage = '<a href="javascript:;">第一页</a>';
        $prevPage = '<a href="javascript:;">上一页</a>';
    }else{
        $homePage = '<a href="'.$url.'">第一页</a>';
        $prevPage = '<a href="'.$url.$ext.($page-1).'/">上一页</a>';
    }

    if($page == $total){
        $endPage = '<a href="javascript:;">最后一页</a>';
        $nextPage = '<a href="javascript:;">下一页</a>';
    }else{
        $endPage = '<a href="'.$url.$ext.$total.'/">最后一页</a>';
        $nextPage = '<a href="'.$url.$ext.($page+1).'/">下一页</a>';
    }

    $numPage = '';
    if($count > 5){
        if(($page-2)<1){
            $numStart = 1;
        }else{
            $numStart = $page-2;
        }
        if(($page+2)>$total){
            $numEnd = $total;
        }else{
            $numEnd = $page+2;
        }
        for($i=$numStart;$i<=$numEnd;$i++){
            if($page == $i){
                $numPage .= '<span>'.$i.'</span>';
            }else{
                $numPage .= '<a href="'.$url.$ext.$i.'/">'.$i.'</a>';
            }
        }
    }else{
        for($i=1;$i<=$total;$i++){
            if($page == $i){
                $numPage .= '<span>'.$i.'</span>';
            }else{
                $numPage .= '<a href="'.$url.$ext.$i.'/">'.$i.'</a>';
            }
        }
    }
    
    $pages = $homePage.$prevPage.$numPage.$nextPage.$endPage;
    return $pages;
}
//生成移动端分页
function makePagesM($url,$page,$total,$count){
    if($count == 0){
        return;
    }
    $ext = 'p';
    if($page == 1){
        $prevPage = '<a class="prev disable">上一页</a>';
    }else{
        $prevPage = '<a href="'.$url.$ext.($page-1).'/" class="prev">上一页</a>';
    }

    if($page == $total){
        $nextPage = '<a class="next disable">下一页</a>';
    }else{
        $nextPage = '<a href="'.$url.$ext.($page+1).'/" class="next">下一页</a>';
    }

    $info = '<div class="ym">
                <span class="current">'.$page.'</span>/<span class="total">'.$total.'</span>
            </div>';
    
    $pages = $prevPage.$info.$nextPage;
    return $pages;
}
//获取友情链接
function getFriendlinks(){
    $lists = Db::name('friendlinks')->where('status',1)->select()->toArray();
    return $lists;
}
//获取餐厅链接
function getCantingUrl($location){
    $url = 'javascript:;';
    $row = Db::name('dp_cyms')->where('address',$location)->find();
    if(!empty($row)){
        $area1 = Db::name('area')->field('pinyin')->where('id',$row['areaid1'])->find();
        $area2 = Db::name('area')->field('pinyin')->where('id',$row['areaid2'])->find();
        $cate = Db::name('articles_cate')->field('dirname')->where('id',$row['cate_id'])->find();
        $url = '/'.$area1['pinyin'].'_'.$area2['pinyin'].'/'.$cate['dirname'].'/'.$row['id'].'.html';
    }
    return $url;
}
//获取酒店链接
function getJiudianUrl($location){
    $url = 'javascript:;';
    $row = Db::name('dp_jdzs')->where('address',$location)->find();
    if(!empty($row)){
        $area1 = Db::name('area')->field('pinyin')->where('id',$row['areaid1'])->find();
        $area2 = Db::name('area')->field('pinyin')->where('id',$row['areaid2'])->find();
        $cate = Db::name('articles_cate')->field('dirname')->where('id',$row['cate_id'])->find();
        $url = '/'.$area1['pinyin'].'_'.$area2['pinyin'].'/'.$cate['dirname'].'/'.$row['id'].'.html';
    }
    return $url;
}
//获取天气
// function getWeather($city){
// 	$key = "ae6668dfc165182e74d97b3f017ee575";
// 	$url = "https://restapi.amap.com/v3/weather/weatherInfo?key=$key&city=$city";
// 	$json = file_get_contents($url);
// 	$data = json_decode($json,true);
// 	if(!empty($data['lives'])){
// 		return $data['lives'][0];
// 	}
// 	return null;
// }
function getWeather($pname,$cityname){
    $area = $pname.$cityname;
    $pasttime = (time()-(60*5));
    $tianqi = Db::name('tianqi')->field('weather')->where('update_time','>',$pasttime)->find($area);
    if(empty($tianqi)){
        $weather = tianqiApi($cityname,$pname);
        Db::name('tianqi')->replace()->insert(['area'=>$area,'weather'=>$weather,'update_time'=>time()]);
    }else{
        $weather = $tianqi['weather'];
    }
    return json_decode($weather,true);
}
//获取邮编
function getZipcode($area){
    $data = Db::name("area")->field('zip')->where('name',$area)->find();
    if(empty($data)){
        return '';
    }
    return $data['zip'];
}
//获取星期几
function getWeek($time){
    $weekarray=array("日","一","二","三","四","五","六");
    return "星期".$weekarray[date("w",$time)];
}